package com.xunshibao.service.user.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xunshibao.dao.user.UserDao;
import com.xunshibao.entity.user.Profile;
import com.xunshibao.entity.user.User;
import com.xunshibao.entity.user.UserLogin;
import com.xunshibao.service.user.UserDRO;
import com.xunshibao.service.user.UserService;
import com.xunshibao.util.DateUtil;

@Service
public class UserServiceImpl implements UserService {
	private UserDao userDao;

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public boolean register(String uname, String pwd, String email) {
		UserLogin login = new UserLogin(uname, pwd, "1");
		if (userDao.save(login)) {
			User user = new User();
			user.setId(login.getUid());
			user.setEmail(email);
			Profile profile = new Profile();
			profile.setUid(login.getUid());

			return userDao.save(user) && userDao.save(profile);
		}
		return false;
	}

	@Override
	public UserDRO login(String username, String password) {
		UserDRO dro = new UserDRO();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", username);
		params.put("password", password);
		String hql = "select user from UserLogin user where user.username=:username and user.pwd=:password";
		
		UserLogin login = (UserLogin) this.userDao.findSingle(hql, params);
		if(login != null) {
			if(!"0".equals(login.getStatus())) {//用户被锁定
				int now = DateUtil.getNow();
				params = new HashMap<String, Object>();
				params.put("now", now);
				params.put("now", login.getUid());
				hql = "update User u set u.lastvisit=u.thisvisit, u.thisvisit=:now where u.id=:uid";
				this.userDao.hqlUpdate(hql, params);
				dro.setValid(true);
				dro.setLogin(login);
				dro.setUser(this.getUserMsg(login.getUid()));
			}
		}
		
		return dro;
	}

	
	private User getUserMsg(Long uid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		String hql = "select new User(u.id, u.newpm, u.syspm, u.postnum) from User u where u.id=:uid";
		return (User) this.userDao.findSingle(hql, params);
		
	}

	@Override
	public boolean exists(String username) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", username);
		String hql = "select count(u.id) from UserLogin u where u.username=:username";
		Long result = (Long) userDao.findSingle(hql, params);
		if(result != null) {
			if(result.intValue() > 1) {
				return true;
			} else 
				return false;
		}
		return false; //TODO error
	}

	@Override
	public Long getUserIdByName(String username) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", username);
		String hql = "select new UserLogin(login.uid, login.username) from UserLogin login where login.username=:username";
		UserLogin login = (UserLogin) this.userDao.findSingle(hql, params);
		if(login != null) {
			return login.getUid();
		}
		return null;
	}
}
